package com.example.teamtribe.main.club.clubintroduce.media;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.event.Refresh;
import com.example.teamtribe.utils.Utility;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

/**
 * Created by lenovo on 2018/6/10.
 */

public class AddMediaPresenter implements AddMediaContract.Presenter{
    private AddMediaContract.View view;
    private MediaModel model;

    public AddMediaPresenter(AddMediaContract.View view) {
        this.view = view;
        model =MediaModel.getInstance();
    }

    private void add(String name, int teamId, String cover, String describe, final int type) {
        model.addMedia(name, teamId, cover, describe, type, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                view.hideProgress();
                if(type==1)EventBus.getDefault().post(new Refresh(Refresh.ALBUMFRAGMENT));
                else EventBus.getDefault().post(new Refresh(Refresh.VIDEOFRAGMENT));
                view.next();
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }

    private void upload(final String name, final int teamId, final String cover, final String describe, final int type) {
        view.showProgress();
        File file = new File(cover);
        FileModel.getInstance().upload(Utility.getToken(), file, 0, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                add(name, teamId, bundle.getString("fileName"),describe,type);
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }

    @Override
    public void addMedia(String name, int teamId, String cover, String description, int type) {
        if (TextUtils.isEmpty(cover)) {
            view.showToast("请上传封面");
            return;
        }
        if (TextUtils.isEmpty(name) ) {
            view.showToast("请填写名称");
            return;
        }
        upload(name, teamId, cover, description,type);
    }
}
